import React, { useEffect } from 'react';
import JPush from 'jpush-react-native';
import { ToastProvider } from 'react-native-toast-notifications';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { Provider } from 'react-redux';
import { AppState } from 'react-native';
import NotificationClass from './src/utils/NotificationClass.ts';
import { LogBox } from 'react-native';
import Toast from 'react-native-toast-message';
import { navigationRef } from './src/common/navigationService.ts';
import store, { persistor } from './src/redux/store.ts';
import { Button, NativeBaseProvider } from 'native-base';
import { ignoreLogs } from './src/common/config.ts';
import screenRoute from './screen.ts';
import LoadingOverlay from './src/component/LoadingOverlay.tsx';
import ConfirmDialog from './src/component/ConfirmDialog.tsx';
import RecordingModal from './src/component/RecordingModal.tsx';
import * as WeChat from 'react-native-wechat-lib';
import { PersistGate } from 'redux-persist/integration/react';
import CompPriceDiffPopup from './src/view/home/component/CompPriceDiffPopup.tsx';
import MergerCaseModal from './src/view/tab/home/component/MergerCaseModal.tsx';
import SkuSelectionPopup from './src/view/home/component/SkuSelectionPopup.tsx';
import SkuPackageSelectionPopup from './src/view/home/component/SkuPackageSelectionPopup.tsx';

const Stack = createNativeStackNavigator();
LogBox.ignoreLogs(ignoreLogs);

const connectListener = (result: { connectEnable: boolean }) => {
  console.log('链接状态:' + JSON.stringify(result));
};
const notificationListener = async (notify: NotifyOptions) => {
  console.log('通知回调:' + JSON.stringify(notify));
  if (notify.notificationEventType === 'notificationArrived') {
    await NotificationClass.serverNotification();
  }
  if (notify.notificationEventType === 'notificationOpened') {
    await NotificationClass.notificationOpened();
  }
};
const tagAliasListener = (result: any) => {
  console.log('tagAliasListener:' + JSON.stringify(result));
};
const mobileNumberListener = (result: { code: number }) => {
  console.log('手机号码事件回调:' + JSON.stringify(result));
};

export default function App() {
  useEffect(() => {
    AppState.addEventListener('change', handleAppStateChange);
    // 注册微信
    // WeChat.registerApp('appid');

    WeChat.isWXAppInstalled()
      .then((res) => {
        console.log(res);
      })
      .catch((err) => {
        console.log(err);
      });

    // JPush.init({"appKey": "d0070f1406891c66618bde36", "channel": "default", "production": true});

    // 获取registerID
    // JPush.getRegistrationID(result => console.log("registerID:" + JSON.stringify(result)))
    // 连接状态
    JPush.addConnectEventListener(connectListener);
    // 通知回调
    JPush.addNotificationListener(notificationListener);
    //tag alias事件回调
    JPush.addTagAliasListener(tagAliasListener);
    //手机号码事件回调
    JPush.addMobileNumberListener(mobileNumberListener);
  }, []);

  // 监听应用状态变化
  const handleAppStateChange = (nextAppState: string) => {
    if (nextAppState === 'active') {
      console.log('活动状态');
    }
    if (nextAppState === 'background') {
      console.log('后台状态');
    }
  };

  return (
    <Provider store={store}>
      <PersistGate loading={null} persistor={persistor}>
        <ToastProvider offsetBottom={100}>
          <NativeBaseProvider>
            <NavigationContainer ref={navigationRef}>
              <Stack.Navigator>
                {screenRoute.map((item: any, index) => {
                  return <Stack.Screen options={item.options} name={item.name} component={item.component} key={index} />;
                })}
              </Stack.Navigator>
            </NavigationContainer>
            <Toast />
            <LoadingOverlay />
            <RecordingModal />
            <ConfirmDialog />
            <CompPriceDiffPopup />
            <MergerCaseModal />
            <SkuSelectionPopup />
            <SkuPackageSelectionPopup />
          </NativeBaseProvider>
        </ToastProvider>
      </PersistGate>
    </Provider>
  );
}
